//
//  NNWaterFlowLayoutController.swift
//  CommonTool
//
//  Created by tenroadshow on 12.1.22.
//

import UIKit

class NNWaterFlowLayoutController: NNBaseViewController {

    
    lazy var collectionView: UICollectionView = {
        let layout = NNCollectionViewFlowLayout()
        layout.delegate = self
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.dataSource = self
        collectionView.delegate   = self
        collectionView.backgroundColor = .white
        collectionView.register(NNWaterFlowCollectionCell.self,
                                forCellWithReuseIdentifier: NNWaterFlowCollectionCell.reuseId)
        return collectionView
    }()
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "瀑布流"
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}


extension NNWaterFlowLayoutController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 100
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NNWaterFlowCollectionCell.reuseId,
                                                      for: indexPath) as! NNWaterFlowCollectionCell
        return cell
    }
}

extension NNWaterFlowLayoutController: UICollectionViewDelegate {
    
}


extension NNWaterFlowLayoutController: NNCollectionViewFlowLayoutDelegate {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: NNCollectionViewFlowLayout, heightForRowAt indexPath: IndexPath, and currentCellWidth: CGFloat) -> CGFloat {
        return  CGFloat.random(in: 50...250)
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 5
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: NNCollectionViewFlowLayout, numberOfColumAt section: Int) -> Int {
        
        if section == 0 {
            return 1
        }else if section == 1 {
            return 2
        }else {
            return 3
        }
    }
    
}

class NNWaterFlowCollectionCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setupUI() {
        contentView.backgroundColor = .random
    }
}
